package com.hx.myapp;

import static android.app.ProgressDialog.STYLE_HORIZONTAL;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import util.ToastUtil;

public class ProgressActivity extends AppCompatActivity implements View.OnClickListener{
     private ProgressBar mPb3;
     private Button mBtnStart,mBtnProgressDialog1,mBtnProgressDialog2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        mPb3 = findViewById(R.id.pb3);
        mBtnStart = findViewById(R.id.btn_start);
        mBtnProgressDialog1 = findViewById(R.id.btn_progress_dialog1);
        mBtnProgressDialog2 = findViewById(R.id.btn_progress_dialog2);
        mBtnStart.setOnClickListener(this) ;
        mBtnProgressDialog1.setOnClickListener(this) ;
        mBtnProgressDialog2.setOnClickListener(this) ;
    }
    Handler handler = new Handler(Looper.myLooper()){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(mPb3.getProgress()<100){
                handler.postDelayed(runnable,500);
            }else{
                ToastUtil.showMag(ProgressActivity.this,"加载完成");
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mPb3.setProgress(mPb3.getProgress()+5);
            handler.sendEmptyMessage(0);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
                handler.sendEmptyMessage(0);
                break;
            case R.id.btn_progress_dialog1:
                ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在加载");
                progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        ToastUtil.showMag(ProgressActivity.this,"cancel");
                    }
                });
                progressDialog.setCancelable(false);
                progressDialog.show();
                break;
            case R.id.btn_progress_dialog2:
                ProgressDialog progressDialog2 = new ProgressDialog(ProgressActivity.this);
                progressDialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog2.setTitle("提示");
                progressDialog2.setMessage("正在下载。。。");
                progressDialog2.setButton(DialogInterface.BUTTON_POSITIVE, "棒", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressDialog2.dismiss();
                    }
                });
                progressDialog2.show();
                break;
        }
    }
}